package com.jzhao.jpre.base;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;

public class StringPublicDeal_bak {
	private String encoding;

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String convToGB2312(String str) {
		if (str == null || encoding.equals("GB2312"))
			return str;
		try {
			return new String(str.getBytes(encoding), "GB2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}

	public String convFromGB2312(String str) {
		if (str == null || encoding.equals("GB2312"))
			return str;
		try {
			return new String(str.getBytes("GB2312"), encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}

	public String convToISO8859(String str) {
		if (str == null || str.length() == 0)
			return str;
		try {
			return new String(str.getBytes("ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}
	
	//add by henry
	public String convToISO885901(String str) {
		if (str == null || encoding.equals("ISO-8859-1")||str.length() == 0)
			return str;
		try {
			return new String(str.getBytes(encoding), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}
	
	//add by henry
	public String convFromISO885901(String str) {
		if (str == null || encoding.equals("ISO-8859-1"))
			return str;
		try {
			return new String(str.getBytes("ISO-8859-1"), encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return str;
		}
	}
	
	//add by henry
	public byte[] convToByteFrom8859(String str) {
		if (str == null ||str.length() == 0)
			return null;
		try {
			int i=0;
			return str.getBytes("ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//add by henry
	public String convTo8859FromByte(byte[] data) {
		if (data==null)
			return null;
		try {
			return new String(data,"ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String convDateToYYYYMMDD(String yyyy_MM_dd) {
		if (yyyy_MM_dd != null && yyyy_MM_dd.length() > 9) {
			StringBuffer date = new StringBuffer();
			date.append(yyyy_MM_dd.substring(0, 4));
			date.append(yyyy_MM_dd.substring(5, 7));
			date.append(yyyy_MM_dd.substring(8, 10));
			return date.toString();
		}
		return yyyy_MM_dd;
	}

	public String convDateToyyyy_MM_dd(String yyyyMMdd) {
		if (yyyyMMdd != null && yyyyMMdd.length() > 7) {
			StringBuffer date = new StringBuffer();
			date.append(yyyyMMdd.substring(0, 4));
			date.append("-");
			date.append(yyyyMMdd.substring(4, 6));
			date.append("-");
			date.append(yyyyMMdd.substring(6, 8));
			return date.toString();
		}
		return yyyyMMdd;
	}

	public String trim(String str) {
		if (str == null || str.length() == 0)
			return "";
		return str.trim();
	}

	public String rtrim(String str) {
		if (str == null || str.length() == 0)
			return "";
		StringUtils.stripEnd(str, " ");
		//return str.trim();
		return str;
	}

	public String formatMoney(String str) {
		try {
			if (str != null && str.trim().length() != 0) {
				BigDecimal b = new BigDecimal(str.trim());
				str = b.movePointLeft(2).toString();
			} else {
				str = "0.00";
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return str;
	}

	public String unFormatMoney(String str) {
		try {
			if (str != null && str.trim().length() != 0) {
				BigDecimal b = new BigDecimal(str.trim());
				str = b.movePointRight(2).toString();
			} else {
				str = "0";
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return str;
	}

	public String filterErrmsg(String str, String filter[]) {
		String errMsg = str;
		for (int i = 0; i < filter.length; i++) {
			while (errMsg.indexOf(filter[i]) != -1) {
				int pos = errMsg.indexOf(filter[i]);
				errMsg = errMsg.substring(0, pos)
						+ errMsg.substring(pos + filter[i].length());
			}
		}
		return errMsg;
	}
}
